import { getCampusSelector } from '@/api/campus/campus'
import OrgSelect from '@/components/Process/OrgSelect'
import request from '@/utils/request'
export default {
  components: { OrgSelect },
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      dataForm: {
        enCode: undefined,
        fullName: undefined,
        area: undefined,
        contactList: undefined,
        brand: undefined,
        campusId: undefined,
        emailTo: undefined
      },
      submitForm: {
        id: undefined,
        time: undefined, // 转让时间
        description: undefined, //转让说明
        campusId: undefined, // 校区ID
        appendix: undefined, // 附件数组
        emailTo: [],
        contacts: [
          {
            name: undefined,
            mobile: undefined,
            gender: undefined,
            idNumber: undefined,
            qq: undefined,
            email: undefined,
            wechat: undefined,
            isSigner: undefined,
            description: undefined,
            signingType: 'personal', // 默认为个人签约
            // 企业签约相关字段
            companyName: undefined, // 公司名称
            companyAddress: undefined, // 注册地址
            creditCode: undefined, // 统一社会信用代码
            companyPhone: undefined, // 公司电话
            companyEmail: undefined, // 公司邮箱
            // 法人代表信息
            legalRepName: undefined, // 法人姓名
            legalRepIdNumber: undefined, // 法人身份证
            legalRepMobile: undefined, // 法人电话
            legalRepWechat: undefined // 法人微信
          }
        ]
      },
      // 开始结束日期限制
      pickerOptionsStart2: {
        disabledDate: time => {
          if (this.dataForm.endTime) {
            return time.getTime() >= new Date(this.dataForm.endTime).getTime()
          }
        }
      },
      // 结束日期限制
      pickerOptionsEnd2: {
        disabledDate: time => {
          if (this.dataForm.startDate) {
            return time.getTime() <= new Date(this.dataForm.startDate).getTime()
          }
        }
      },
      rules: {
        time: [
          {
            required: true,
            message: '请选择转让时间',
            trigger: 'change'
          }
        ],
        description: [
          {
            required: true,
            message: '请添加转让说明',
            trigger: 'blur'
          }
        ],
        emailTo: [
          {
            required: true,
            message: '请选择邮件的收件人',
            trigger: 'change'
          }
        ]
      },
      brandOptions: [],
      campusOptions: [],
      genderOptions: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.$store.dispatch('base/getDictionaryData', { sort: 'sex' }).then(res => {
      this.genderOptions = res
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
  },
  mounted() {},
  methods: {
    handleChangeBrand(val) {
      this.dataForm = {
        enCode: undefined,
        fullName: undefined,
        area: undefined,
        contactList: undefined,
        brand: val,
        campusId: undefined
      }
      this.submitForm.campusId = undefined

      const query = {
        brand: val
      }
      getCampusSelector(query).then(res => {
        this.campusOptions = res.data
      })
    },
    initEmailTo(campus) {
      let emailTo = ['433118678402204933', '433117269455799557', '416750280923677957', '416750736257320197', '381956723331564805', '423653212629763333', '416750360632231173', '396078644641924357', '416750479951791365', '416752342017246469', '433119597265159429', '370757840840688901', '416750440911209733', '519711074023376133', '433108138439214341']
      if (campus.company == '河北分公司') {
        emailTo.push('416749733969659141')
        emailTo.push('492107727120631045')
      } else if (campus.company == '山东分公司') {
        emailTo.push('720164055167272197')
        emailTo.push('416748329951560965')
      } else if (campus.company == '西安分公司') {
        emailTo.push('720164338505090309')
        emailTo.push('416748748413076741')
      }
      this.onOrgChange(emailTo)
    },
    handleChangeCampus(val) {
      if (val) {
        this.submitForm.campusId = val
        request({
          url: `api/campus/${val}/info`,
          method: 'get'
        }).then(res => {
          this.dataForm = Object.assign(res.data, { campusId: val })
          this.initEmailTo(res.data)
        })
      }
    },
    onOrgChange(data) {
      this.dataForm.emailTo = data
      this.submitForm.emailTo = data
    },
    onDeleteManage(index) {
      this.$confirm('确定删除该投资人， 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          this.submitForm.contacts.splice(index, 1)
        })
        .catch(() => {})
    },
    onAddManage() {
      this.submitForm.contacts.push({
        name: undefined,
        mobile: undefined,
        gender: undefined,
        idNumber: undefined,
        qq: undefined,
        email: undefined,
        wechat: undefined,
        isSigner: undefined,
        description: undefined,
        signingType: 'personal', // 默认为个人签约
        // 企业签约相关字段
        companyName: undefined,
        companyAddress: undefined,
        creditCode: undefined,
        companyPhone: undefined,
        companyEmail: undefined,
        // 法人代表信息
        legalRepName: undefined,
        legalRepIdNumber: undefined,
        legalRepMobile: undefined,
        legalRepWechat: undefined
      })
    },
    handleSigningTypeChange(contact) {
      // 当签约类型变更时，根据需要重置相关字段
      if (contact.signingType === 'personal') {
        // 切换到个人签约时，清空企业相关字段
        contact.companyName = undefined
        contact.companyAddress = undefined
        contact.creditCode = undefined
        contact.companyPhone = undefined
        contact.companyEmail = undefined
        contact.legalRepName = undefined
        contact.legalRepIdNumber = undefined
        contact.legalRepMobile = undefined
        contact.legalRepWechat = undefined
      }
    },
    goBack() {
      this.$emit('refresh')
    },
    init(id, isDetail) {
      this.dataForm.id = id || ''
      this.visible = true
      this.submitForm.campusId = id
      this.$refs['elSubForm'].resetFields()
    },
    dataFormSubmit() {
      this.$refs['elSubForm'].validate(valid => {
        if (valid) {
          this.loading = true
          request({
            url: `/api/campus/transfer/records`,
            method: 'post',
            data: this.submitForm
          })
            .then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.loading = false
                  this.$emit('refresh', true)
                }
              })
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
